package comment

import (
	"context"
	"strings"
	"time"

	"mall/common/globalkey"
	"mall/common/modelx"
	"mall/common/resultx"
	"mall/service/forum/model"
	"mall/service/forum/rpc/internal/svc"
	"mall/service/forum/rpc/types/forum"

	"github.com/volatiletech/null/v8"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreatePostCommentLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreatePostCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePostCommentLogic {
	return &CreatePostCommentLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// #24.11.01#  文章评论
func (l *CreatePostCommentLogic) CreatePostComment(in *forum.PostCommentInfo) (*forum.BaseIDResp, error) {
	if in.CommentContent == nil {
		return nil, resultx.StatusError(resultx.FORUM_COMMENT_CONTENT_REQUIRED, "")
	} else if len(strings.TrimSpace(*in.CommentContent)) == 0 {
		return nil, resultx.StatusError(resultx.FORUM_COMMENT_CONTENT_REQUIRED, "")
	}
	if in.PostId == nil {
		return nil, resultx.StatusError(resultx.FORUM_POST_ID_REQUIRED, "")
	} else if *in.PostId == 0 {
		return nil, resultx.StatusError(resultx.FORUM_POST_ID_REQUIRED, "")
	}
	if in.UserId == nil {
		return nil, resultx.StatusError(resultx.FORUM_COMMENT_USER_ID_REQUIRED, "")
	} else if *in.UserId == 0 {
		return nil, resultx.StatusError(resultx.FORUM_COMMENT_USER_ID_REQUIRED, "")
	}
	now := time.Now()
	gmtNow := modelx.GetGmtTime(now)
	var comment = &model.ForumComment{
		PostID:         *in.PostId,
		CommentUserID:  *in.UserId,
		CommentContent: *in.CommentContent,
		CommentDate:    now,
		CommentDateGMT: gmtNow,
	}
	if in.ForumId != nil && *in.ForumId > 0 {
		comment.ForumID = *in.ForumId
	} else {
		comment.ForumID = uint64(l.svcCtx.Config.Query.PostForumId)
	}
	if in.ParentId != nil && *in.ParentId > 0 {
		comment.ParentID = *in.ParentId
	}
	if in.NickName != nil && len(strings.TrimSpace(*in.NickName)) > 0 {
		comment.CommentUser = *in.NickName
	}
	if in.UserEmail != nil && len(strings.TrimSpace(*in.UserEmail)) > 0 {
		comment.CommentUserEmail = *in.UserEmail
	}
	if in.UserAvatar != nil && len(strings.TrimSpace(*in.UserAvatar)) > 0 {
		comment.CommentUserURL = *in.UserAvatar
	}
	if in.Ip != nil && len(strings.TrimSpace(*in.Ip)) > 0 {
		comment.CommentUserIP = *in.Ip
	}
	if in.AnonymousUser != nil && *in.AnonymousUser > 0 {
		comment.CommentUserAnonymous = uint8(*in.AnonymousUser)
	}
	if in.CommentType != nil && *in.CommentType > 0 {
		comment.CommentType = uint8(*in.CommentType)
	}
	if in.Status != nil && *in.Status > 0 {
		comment.CommentStatus = uint8(*in.Status)
	} else {
		comment.CommentStatus = uint8(globalkey.StatusPending)
	}
	tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
	if err != nil {
		return nil, resultx.StatusError(resultx.DB_ERROR, "")
	}
	meta := []*model.ForumCommentMetum{}
	if len(in.Meta) > 0 {
		for _, v := range in.Meta {
			if v.Key == nil || v.Value == nil || len(strings.TrimSpace(*v.Key)) == 0 || len(strings.TrimSpace(*v.Value)) == 0 {
				continue
			}

			meta = append(meta, &model.ForumCommentMetum{
				MetaKey:   strings.TrimSpace(*v.Key),
				MetaValue: null.StringFrom(strings.TrimSpace(*v.Value)),
			})
		}
	}
	err = l.svcCtx.ForumComment.Insert(l.ctx, tx, comment, meta)
	if err != nil {
		tx.Rollback()
		return nil, err
	}
	tx.Commit()
	return &forum.BaseIDResp{
		Id: comment.CommentID,
	}, nil
}
